﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_Categories : Common
    {

        public List<CategoryModel> GetCateComboxList(string cate = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Category> queryCate = _context.Library_Categories;

            //Filter
            if (!string.IsNullOrEmpty(cate))
            {
                queryCate = queryCate.Where(c => c.Name.Contains(cate));
            }

            List<Library_Category> categories = queryCate.ToList();

            List<CategoryModel> cateList = new List<CategoryModel>();

            foreach (Library_Category c in categories)
            {
                CategoryModel model = new CategoryModel();
                model.ID = c.Id;
                model.CategoryName = c.Name;
                cateList.Add(model);
            }
            //
            _context.Dispose();
            return cateList;
        }

        public int GetDataCount()
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Category> queryCate = _context.Library_Categories;
            return queryCate.Count();
        }

        public List<CategoryModel> GetDataList(int startIndex = 0, int pageSize = 0, string sorting = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Category> queryCate = _context.Library_Categories;
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
            {
                queryCate = queryCate.OrderBy(c => c.Name);
            }
            else if (sorting.Equals("Name DESC"))
            {
                queryCate = queryCate.OrderByDescending(c => c.Name);
            }
            List<Library_Category> categories = pageSize > 0 ?
                                                queryCate.Skip(startIndex).Take(pageSize).ToList<Library_Category>()
                                                : queryCate.ToList<Library_Category>();
            List<CategoryModel> cateList = new List<CategoryModel>();

            foreach (Library_Category lc in categories)
            {
                CategoryModel data = new CategoryModel();
                data.ID = lc.Id;
                data.CategoryName = lc.Name;
                data.Description = lc.Description;
                cateList.Add(data);
            }
            //
            _context.Dispose();
            return cateList;
        }

        public CategoryModel AddNewData(CategoryModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Category lc = new Library_Category();
            lc.Name = data.CategoryName;
            lc.Description = data.Description;
            //
            _context.Library_Categories.InsertOnSubmit(lc);
            _context.SubmitChanges();
            //Gán lại giá trị vừa insert cho Model
            data.ID = lc.Id;
            //
            _context.Dispose();
            return data;
        }

        public bool UpdateData(CategoryModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Category lc = _context.Library_Categories.Where(c => c.Id == data.ID).FirstOrDefault<Library_Category>();
            //
            lc.Name = data.CategoryName;
            lc.Description = data.Description;
            //
            _context.SubmitChanges();
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Category lc = _context.Library_Categories.Where(c => c.Id == Id).FirstOrDefault<Library_Category>();
            //
            _context.Library_Categories.DeleteOnSubmit(lc);
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }
    }
}